package com.blb.ssm.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.ssm.controller.base.BaseController;
import com.blb.ssm.entity.Menu;
import com.blb.ssm.entity.RoleMenu;
import com.blb.ssm.service.IMenuService;
import com.blb.ssm.service.IRoleMenuService;
import com.blb.ssm.utils.IconFontUtils;
import com.blb.ssm.utils.PageEntity;
import com.blb.ssm.utils.ResponseEntity;
import com.blb.ssm.utils.UUIDUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huahao
 * @since 2019-01-15
 */
@Controller
@RequestMapping("/a/menu")
public class MenuController extends BaseController {

	@Autowired
	private IMenuService menuService;
	
	@Autowired
	private IRoleMenuService roleMenuService;
	
	@RequiresPermissions("menu:list")
	@RequestMapping("listPage")
	public String listPage(String id, Model model){
		List<Menu> menus = menuService.list(new QueryWrapper<Menu>().eq("pid", id).orderByAsc("seq"));
		model.addAttribute("menus", menus);
		return disPlay(ADMIN, "menu", "menu_list");
	}
	
	@RequestMapping("addPage")
	public String addPage(Model model){
		//父级菜单
		List<Menu> list = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", '0'));
		findChildrens(list);
		
		//图标
		List<String> iconFont = IconFontUtils.getIconFont();
		
		model.addAttribute("iconFont", iconFont);
		model.addAttribute("list", list);
		return disPlay(ADMIN, "menu", "menu_add");
	}

	private void findChildrens(List<Menu> list){
		for (Menu menu : list) {
			List<Menu> list2 = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", menu.getId()));
			if (list2!=null) {
				menu.setNodes(list2);
				findChildrens(list2);
			}
		}
	}
	
	/**
	 * 分页数据
	 * @param page
	 * @param limit
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list")
	public PageEntity menuList(int page, int limit, Menu menu){
		List<Menu> list = menuService.list(new QueryWrapper<Menu>().eq("pid", "0").orderByAsc("seq"));
		List<Menu> list2 = findChildrens2(list, null);
		return new PageEntity(list2.size(), list2);
	}
	
	private List<Menu> findChildrens2(List<Menu> list, List<Menu> list2){
		if (list2==null) {
			list2 = new ArrayList<Menu>();
		}
		for (Menu menu : list) {
			list2.add(menu);
			List<Menu> list3 = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", menu.getId()).orderByAsc("seq"));
			if (list3!=null && !list3.isEmpty()) {
				findChildrens2(list3, list2);
			}
		}
		return list2;
	}
	
	/**
	 * 保存
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public ResponseEntity addMenu(Menu menu){
		menu.setId(UUIDUtils.getID());
		menu.setCreateTime(new Date());
		menuService.save(menu);
		return success();
	}
	
	/**
	 * 删除（支持批量删除）
	 * @param menus
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public ResponseEntity delete(@RequestBody ArrayList<Menu> menus){
		List<String> list = new ArrayList<String>();
		for (Menu menu : menus) {
			list.add(menu.getId());
		}
		menuService.removeByIds(list);
		return success();
	}
	
	/**
	 * 跳转修改界面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("updatePage")
	public String adminPage(String id, Model model){
		Menu menu = menuService.getById(id);
		model.addAttribute("menu", menu);
		
		List<Menu> list = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", '0').orderByAsc("seq"));
		findChildrens(list);
		
		//图标
		List<String> iconFont = IconFontUtils.getIconFont();
		
		model.addAttribute("iconFont", iconFont);
		model.addAttribute("list", list);
		return disPlay(ADMIN, "menu", "menu_update");
	}
	
	/**
	 * 权限列表
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("MenuList")
	public PageEntity MenuList(String id, Model model){
		List<RoleMenu> roleMenuList = roleMenuService.list(new QueryWrapper<RoleMenu>(new RoleMenu()).eq("role_id", id));
		List<Menu> list = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", '0').orderByAsc("seq"));
		List<JSONObject> list2 = findChildrens3(list, roleMenuList, null);
		return new PageEntity(list2.size(), list2);
	}
	
	private List<JSONObject> findChildrens3(List<Menu> list, List<RoleMenu> roleMenuList, List<JSONObject> list2){
		if (list2==null) {
			list2 = new ArrayList<JSONObject>();
		}
		for (Menu menu : list) {
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(menu));
			
			boolean rs = false;
			for (RoleMenu rolemenu : roleMenuList) {
				if (rolemenu.getMenuId().equals(menu.getId())) {
					rs = true;
				}
			}
			jsonObject.put("LAY_CHECKED", rs);
			list2.add(jsonObject);
			List<Menu> list3 = menuService.list(new QueryWrapper<Menu>(new Menu()).eq("pid", menu.getId()).orderByAsc("seq"));
			if (list3!=null && !list3.isEmpty()) {
				findChildrens3(list3, roleMenuList, list2);
			}
		}
		return list2;
	}
	
	/**
	 * 修改
	 * @param menu
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public ResponseEntity updateMenu(Menu menu){
		menu.setUpdateTime(new Date());
		menuService.updateById(menu);
		return success();
	}
}

